var express = require('express');
var router = express.Router();

const mongoose = require('mongoose');
const bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended: false});

const { ensureAuthenticated } = require('../../helps/auth');

/**
 * 引入模型
 * @type models
 */
require('../../models/Idea');
const Idea = mongoose.model('ideas');

/**
 * 请求
 * @type http
 */
router.get('/', function(req, res, next) {
    res.send({
        code: 200,
        data: {name: 'tom'},
        msg: '请求成功！'
    });
});
// 列表
router.get('/list', ensureAuthenticated, (req, res) => {
    Idea.find({user: req.user.id})
        .sort({
            date: 'desc'
        })
        .then(ideas => {
            res.render('ideas/index', {ideas})
        })
    })
// 详情-添加
router.get('/add', (req, res) => {
    res.render('ideas/add');
})
// 保存-添加
router.post('/save', urlencodedParser, (req, res) => {
    let errors = [];

    if(!req.body.title) {
        errors.push({
            text: '请输入标题！'
        })
    }
    if(!req.body.details) {
        errors.push({
            text: '请输入详情！'
        })
    }

    if(errors.length > 0) {
        res.render('ideas/add', {
            errors,
            title: req.body.title,
            details: req.body.details,
        })
    }else{
        const newUser = {
            title: req.body.title,
            details: req.body.details,
            user: req.user.id
        }

        new Idea(newUser).save()
        .then(idea => {
            req.flash('success_msg', '添加成功！')
            res.redirect('/ideas/list')
        })
    }
})
// 详情-编辑
router.get('/find/:id', (req ,res) => {
    Idea.findOne({_id: req.params.id})
        .then(idea => {
            if(idea.user != req.user.id) {
                req.flash('error_msg', '非法操作！');
                res.redirect('/ideas/list')
            }else{
                res.render('ideas/edit', {idea})
            }
        })
})
// 更新-编辑
router.put('/update/:id', urlencodedParser, (req, res) => {
    let errors = [];

    if(!req.body.title) {
        errors.push({
            text: '请输入标题！'
        })
    }
    if(!req.body.details) {
        errors.push({
            text: '请输入详情！'
        })
    }
    if(errors.length > 0) {
        res.render('ideas/edit', {
            errors,
            idea: {
                id: req.params.id,
                title: req.body.title,
                details: req.body.details
            }
        })
    }else{
        Idea.findOne({
            _id: req.params.id
        }).then(idea => {
            idea.title = req.body.title;
            idea.details = req.body.details;

            idea.save().then(() => {
                req.flash('success_msg', '编辑成功！')
                res.redirect('/ideas/list')
            })
        })
    }
})
// 删除
router.delete('/del/:id', ensureAuthenticated, urlencodedParser, (req, res) => {
    Idea.remove({
        _id: req.params.id
    }).then(() => {
        req.flash('success_msg', '删除成功啦！')
        res.redirect('/ideas/list')
    })
})

module.exports = router;
